import 'package:flutter/material.dart';

class Model {
  late int value = 0;

  Model(this.value);
}

class DataInheritedWidget<T> extends InheritedWidget {
  //持有的数据源,由父widget赋值
  final T data;

  //父widget的更新方法
  final Function? update;

  const DataInheritedWidget(Widget child, this.data, this.update, {Key? key})
      : super(child: child, key: key);

  @override
  bool updateShouldNotify(covariant InheritedWidget oldWidget) {
    // 此处可以选择是否通知依赖InheritedWidget的子widget集合进行更新,
    // debugPrint("old value == ${((oldWidget as DataInheritedWidget).data as Model).value}");
    // debugPrint("new value == ${(data as Model).value}");
    return true;

    // return ((oldWidget as DataInheritedWidget).data as Model).value != (data as Model).value;
  }
}

class InheritedParentWidget<T> extends StatefulWidget {
  final Widget child;
  final T data;

  const InheritedParentWidget({required this.child, required this.data, Key? key})
      : super(key: key);

  //仅获取InheritedParentWidget中的数据,
  static T? readData<T>(BuildContext context) {
    //context Element实现了BuildContext抽象类,此处context就是InheritedParentWidget子widget对应的Element,
    return (context.getElementForInheritedWidgetOfExactType<DataInheritedWidget<T>>()?.widget
            as DataInheritedWidget<T>?)
        ?.data;
  }

  //仅获取InheritedParentWidget中的数据,并对InheritedParentWidget进行依赖监听
  //context 同上
  static T? watchData<T>(BuildContext context) {
    return context.dependOnInheritedWidgetOfExactType<DataInheritedWidget<T>>()?.data;
  }

  static void update<T>(BuildContext context, int newValue) {
    var model = InheritedParentWidget.readData<Model>(context);
    model?.value = newValue;

    (context.getElementForInheritedWidgetOfExactType<DataInheritedWidget<T>>()?.widget
            as DataInheritedWidget<T>?)
        ?.update
        ?.call();
  }

  @override
  State<StatefulWidget> createState() {
    return InheritedParent<T>();
  }
}

class InheritedParent<T> extends State<InheritedParentWidget> {
  void update() {
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return DataInheritedWidget<T>(widget.child, widget.data, update);
  }
}

class TestInheritedWidgetStateBuilder extends StatefulWidget {
  const TestInheritedWidgetStateBuilder({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return TestInheritedWidgetStateBuilderState();
  }
}

class TestInheritedWidgetStateBuilderState extends State<TestInheritedWidgetStateBuilder> {
  var model = Model(0);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: InheritedParentWidget(
          data: model,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: const [
              //这里使用StatefulBuilder 模拟StatefulWidget实例,主要是方便观察
              // StatefulBuilder(
              //   builder: (BuildContext context, StateSetter setState) {
              //     debugPrint("StatefulBuilder--tip");
              //     return Text("当前数值:${InheritedParentWidget.of<Model>(context)?.value}");
              //   },
              // ),
              DisplayWidget(),

              //这里使用StatefulBuilder 模拟StatefulWidget实例,主要是方便观察
              // StatefulBuilder(
              //   builder: (BuildContext context, StateSetter setState) {
              //     debugPrint("StatefulBuilder--click");
              //     return TextButton(
              //         onPressed: () {
              //           var model = InheritedParentWidget.ofData<Model>(context);
              //           model?.value = model.value + 1;
              //           InheritedParentWidget.update<Model>(context);
              //         },
              //         child: Text("--点击--:${InheritedParentWidget.ofData<Model>(context)?.value}"));
              //   },
              // ),
              CounterBtnWidget(),
            ],
          )),
    );
  }
}

class DisplayWidget extends StatefulWidget {
  const DisplayWidget({Key? key}) : super(key: key);

  @override
  State<DisplayWidget> createState() => _DisplayWidgetState();
}

class _DisplayWidgetState extends State<DisplayWidget> {
  @override
  Widget build(BuildContext context) {
    debugPrint("StatefulBuilder--tip");
    return Text("当前数值:${InheritedParentWidget.watchData<Model>(context)?.value}");
  }
}

class CounterBtnWidget extends StatefulWidget {
  const CounterBtnWidget({Key? key}) : super(key: key);

  @override
  State<CounterBtnWidget> createState() => _CounterBtnWidgetState();
}

class _CounterBtnWidgetState extends State<CounterBtnWidget> {
  @override
  Widget build(BuildContext context) {
    debugPrint("StatefulBuilder--click");
    return TextButton(
        onPressed: () {
          var oldModel = InheritedParentWidget.readData<Model>(context);
          InheritedParentWidget.update<Model>(context, (oldModel?.value ?? 0) + 1);
        },
        child: Text("--点击--:${InheritedParentWidget.readData<Model>(context)?.value}"));
  }
}
